/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.*;
import pojo.*;
import utils.ClassUtils;
import utils.HibernateUtil;

/**
 *
 * @author DANG
 */
public class ChoNgoiDAO {

    public static SessionFactory _localSessionFactory;
    public static final String CODE_PLACE_TEMPLATE = "P%d%d%d%d";
    public static Session _localSession;

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }

    public static void openCurrentSession() throws Exception {
        try {
            if (_localSession != null) {
                _localSession.close();
            }
            _localSession = _localSessionFactory.openSession();
        } catch (Exception e) {
            throw new Exception(String.format("Failes in %s of %s : %s", "openCurrentSession", PhimDAO.class.getName(), e.getMessage()));
        }
    }

    public static void closeCurrentSession() throws Exception {
        if (_localSession == null) {
            return;
        }
        try {
            _localSession.close();
        } catch (Exception e) {
            throw new Exception(String.format("Failes in %s of %s : %s", "closeCurrentSession", PhimDAO.class.getName(), e.getMessage()));
        } finally {
            _localSession = null;
        }
    }

    public static String generateCodePlace(int roomID, int indexLine, int indexCol) {
        String rs = String.format(CODE_PLACE_TEMPLATE, roomID, indexLine, indexCol, System.nanoTime());
        return rs;
    }

    public static List<Chongoi> generateChoNgoisFromPhongChieu(Phongchieu paramRoom) throws Exception {
        List<Chongoi> items = null;
        if (paramRoom == null) {
            throw new Exception("Param not valid");
        }
        int roomID = paramRoom.getIdPhongChieu();
        int line = paramRoom.getSoLuongHangGhe();
        int col = paramRoom.getSoLuongCot();
        int maxPlace = paramRoom.getSoLuongCho();
        if (maxPlace > line * col) {
            throw new Exception("Place over flow");
        }
        items = new ArrayList<Chongoi>();
        int index = 0;
        for (int i = 0; i < line; i++) {
            for (int j = 0; j < col; j++) {
                index++;
                Chongoi temp = new Chongoi(paramRoom, generateCodePlace(roomID, line, col), ClassUtils.PlaceTypes.STANDARD_TYPE,
                        index, i, ClassUtils.PlacePrices.STANDARD_PRICE, ClassUtils.StateFields.DEFAULT_STATUS, null);
                items.add(temp);
            }
        }
        return items;
    }

    public static List<Chongoi> getChoNgoiByPhongChieuExt(Phongchieu paramRoom) throws Exception {
        List<Chongoi> items = null;
//        Session localSession = null;
        String localQuery = "FROM Chongoi c WHERE c.phongchieu = :room  AND c.trangThai = :statusChoNgoi";
        try {
            //Step 01: validate input params
            if (paramRoom == null) {
                throw new Exception("Param not valid");
            }
            Phongchieu localRoom = paramRoom;
            if (_localSession == null) {
                openCurrentSession();
            }
            //Step 03: begin retrieving transaction in above session.
            _localSession.beginTransaction();
            //Step 04: set parameters retrieving objects.
            Query q = _localSession.createQuery(localQuery);
            q.setEntity("room", localRoom);
            q.setInteger("statusChoNgoi", 1);
            items = q.list();
            //Step 05: commit and end retrieving transaction in above session.
            _localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgoiByPhongChieu", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
//            try {
//                //Step 06: close retrieving session.
//                localSession.close();
//            } catch (Exception e) {
//                throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgoiByPhongChieu", ChoNgoiDAO.class.getName(), e.getMessage()));
//            }
        }
        return items;
    }

    public static List<Chongoi> getChoNgoiByPhongChieu(Phongchieu paramRoom) throws Exception {
        List<Chongoi> items = null;
        Session localSession = null;
        String localQuery = "FROM Chongoi c WHERE c.phongchieu = :room  AND c.trangThai = :statusChoNgoi";
        try {
            //Step 01: validate input params
            if (paramRoom == null) {
                throw new Exception("Param not valid");
            }
            Phongchieu localRoom = paramRoom;
            //Step 02: create retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 04: set parameters retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setEntity("room", localRoom);
            q.setInteger("statusChoNgoi", 1);
            items = q.list();
            //Step 05: commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgoiByPhongChieu", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close retrieving session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgoiByPhongChieu", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static List<Chongoi> getChoNgois() throws Exception {
        List<Chongoi> items = null;
        Session localSession = null;
        String localQuery = "FROM Chongoi c WHERE c.trangThai =:statusChoNgoi ";
        try {
            //Step 01: create retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 02: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 03: set parameters retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setInteger("statusChoNgoi", 1);
            items = q.list();
            //Step 04: commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 05: close retrieving session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static List<Chongoi> getChoNgoisByCodeChoNgoi(String codeChoNgoi) throws Exception {
        List<Chongoi> items = null;
        Session localSession = null;
        String localQuery = "FROM Chongoi c WHERE c.codeChoNgoi = : codeChoNgoi  AND c.trangThai = : statusChoNgoi";
        try {
            //Step 01: validate input params
            if (codeChoNgoi.isEmpty()) {
                throw new Exception("Param not valid");
            }
            //Step 02: create retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 04: set parameters retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setString("codeChoNgoi", codeChoNgoi);
            q.setInteger("statusChoNgoi", 1);
            items = q.list();
            //Step 05: commit and end retrieving transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgoisByCodeChoNgoi", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close retrieving session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getChoNgoisByCodeChoNgoi", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static boolean insertManyChoNgoi(List<Chongoi> paramPlaces) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramPlaces == null) {
                throw new Exception("Param not valid");
            }
            if (paramPlaces.size() < 1) {
                throw new Exception("Places list is empty");
            }
            //Step 02: create insert session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin insert transaction in above session.
            localSession.beginTransaction();
            //Step 04: insert object into database.
            int index = 0;
            for (Chongoi place : paramPlaces) {
                index++;
                localSession.save(place);
                if (index % 20 == 0) {
                    localSession.flush();
                    localSession.clear();
                }
            }
            //Step 05: commit and end insert transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            localSession.getTransaction().rollback();
            throw new Exception(String.format("Failed in %s of %s : %s ", "insertChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "insertChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean insertChoNgois(Chongoi paramChoNgois) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramChoNgois == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create insert session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin insert transaction in above session.
            localSession.beginTransaction();
            //Step 04: insert object into database.
            localSession.save(paramChoNgois);
            //Step 05: commit and end insert transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "insertChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "insertChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean updateChoNgois(Chongoi paramChoNgois) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramChoNgois == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create update session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin update transaction in above session.
            localSession.beginTransaction();
            //Step 04: update object into database.
            localSession.update(paramChoNgois);
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s :  %s", "updateChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s :  %s", "updateChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean deleteManyChoNgoi(List<Chongoi> paramPlaces) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramPlaces == null) {
                throw new Exception("Param not valid");
            }
            if (paramPlaces.size() < 1) {
                throw new Exception("Places list is empty");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: delete object into database.
            int index = 0;
            for (Chongoi place : paramPlaces) {
                index++;
                localSession.delete(place);
                if (index % 20 == 0) {
                    localSession.flush();
                    localSession.clear();
                }
            }
            //Step 05: commit and end delete transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            localSession.getTransaction().rollback();
            throw new Exception(String.format("Failed in %s of %s : %s ", "deleteChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "deleteChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean deleteChoNgois(Chongoi paramChoNgois) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramChoNgois == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: delete object into database.
            localSession.delete(paramChoNgois);
            //Step 05: commit and end delete transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s ", "deleteChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close delete session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s ", "deleteChoNgois", ChoNgoiDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }
}
